package com.yyj.util;

import com.yyj.entity.HttpGetWithEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.*;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * TODO
 *
 * @author chenxingming <1301562@qq.com>
 * @date 2022/10/16
 */
@Slf4j
public class HttpClientUtil {
    public static String doPost(String url, String params) {
        try {
            CloseableHttpClient client = HttpClientBuilder.create().build();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
            StringEntity stringEntity = new StringEntity(params, ContentType.APPLICATION_JSON);
            httpPost.setEntity(stringEntity);
            CloseableHttpResponse response = client.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();
            return EntityUtils.toString(responseEntity);
        } catch (IOException e) {
            return "";
        }
    }

    public static String doPost(String url, Map<String, String> headers, String params) {
        try {
            CloseableHttpClient client = HttpClientBuilder.create().build();
            HttpPost httpPost = new HttpPost(url);
            if (!CheckIsNullOrEmptyUtil.isNullOrEmpty(headers)) {
                headers.forEach(httpPost::setHeader);
            }
            StringEntity stringEntity = new StringEntity(params, ContentType.APPLICATION_JSON);
            httpPost.setEntity(stringEntity);
            CloseableHttpResponse response = client.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();
            return EntityUtils.toString(responseEntity);
        } catch (IOException e) {
            return "";
        }
    }

    public static String doFormPost(String url) {
        try {
            CloseableHttpClient client = HttpClientBuilder.create().build();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
            CloseableHttpResponse response = client.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();
            return EntityUtils.toString(responseEntity);
        } catch (IOException e) {
            return "";
        }
    }

    public static String doFormPost(String url, Map<String, String> headers) {
        try {
            CloseableHttpClient client = HttpClientBuilder.create().build();
            HttpPost httpPost = new HttpPost(url);
            if (!CheckIsNullOrEmptyUtil.isNullOrEmpty(headers)) {
                headers.forEach(httpPost::setHeader);
            }
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
            CloseableHttpResponse response = client.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();
            return EntityUtils.toString(responseEntity);
        } catch (IOException e) {
            return "";
        }
    }


    public static String doGet(String url) {
        try {
            CloseableHttpClient client = HttpClientBuilder.create().build();
            HttpGet httpGet = new HttpGet(url);
            CloseableHttpResponse response;
            response = client.execute(httpGet);
            HttpEntity responseEntity = response.getEntity();
            return EntityUtils.toString(responseEntity);
        } catch (IOException e) {
            return "";
        }
    }

    public static String doGet(String url, Map<String, String> headers, String params) {
        try {
            CloseableHttpClient client = HttpClientBuilder.create().build();
            HttpGetWithEntity httpGet = new HttpGetWithEntity(url);
            if (!CheckIsNullOrEmptyUtil.isNullOrEmpty(headers)) {
                headers.forEach(httpGet::setHeader);
            }
            StringEntity stringEntity = new StringEntity(params, ContentType.APPLICATION_JSON);
            httpGet.setEntity(stringEntity);
            CloseableHttpResponse response;
            response = client.execute(httpGet);
            HttpEntity responseEntity = response.getEntity();
            return EntityUtils.toString(responseEntity);
        } catch (IOException e) {
            return "";
        }
    }

    public static String doGet(String url, String params) {
        try {
            CloseableHttpClient client = HttpClientBuilder.create().build();
            HttpGetWithEntity httpGet = new HttpGetWithEntity(url);
            CloseableHttpResponse response;
            httpGet.setHeader("Content-Type", "application/json;charset=UTF-8");
            StringEntity stringEntity = new StringEntity(params, ContentType.APPLICATION_JSON);
            httpGet.setEntity(stringEntity);
            response = client.execute(httpGet);
            HttpEntity responseEntity = response.getEntity();
            return EntityUtils.toString(responseEntity);
        } catch (IOException e) {
            return "";
        }
    }

//    public static String doGet(String url, Map<String, String> headers, Map<String, Object> param) {
//        try {
//            StringBuilder urlBuilder = new StringBuilder(url);
//            for (Map.Entry<String, Object> entry : param.entrySet()) {
//                if (!CheckIsNullOrEmptyUtil.isNullOrEmpty(entry.getValue())) {
//                    if (urlBuilder.toString().contains("?")) {
//                        urlBuilder.append("&");
//                    } else {
//                        urlBuilder.append("?");
//                    }
//                    String value = URLEncoder.encode( entry.getValue().toString(), StandardCharsets.UTF_8);
//                    urlBuilder.append(entry.getKey()).append("=").append(value);
//                }
//            }
//
//            String resultUrl = urlBuilder.toString().replace("\"", "");
//            log.info("httpClient doGet {}", resultUrl);
//            CloseableHttpClient client = HttpClientBuilder.create().build();
//            HttpGet httpGet = new HttpGet(resultUrl);
//            if (!CheckIsNullOrEmptyUtil.isNullOrEmpty(headers)) {
//                headers.forEach(httpGet::setHeader);
//            }
//            CloseableHttpResponse response = client.execute(httpGet);
//            HttpEntity responseEntity = response.getEntity();
//            return EntityUtils.toString(responseEntity);
//        } catch (IOException e) {
//            return "";
//        }
//    }

    public static String doDelete(String url) {
        try {
            CloseableHttpClient client = HttpClientBuilder.create().build();
            HttpDelete httpDelete = new HttpDelete(url);
            CloseableHttpResponse response;
            response = client.execute(httpDelete);
            HttpEntity responseEntity = response.getEntity();
            return EntityUtils.toString(responseEntity);
        } catch (IOException e) {
            return "";
        }
    }

    public static String doPut(String url, String params) {
        try {
            CloseableHttpClient client = HttpClientBuilder.create().build();
            HttpPut httpPut = new HttpPut(url);
            httpPut.setHeader("Content-Type", "application/json;charset=UTF-8");
            StringEntity stringEntity = new StringEntity(params, ContentType.APPLICATION_JSON);
            httpPut.setEntity(stringEntity);
            CloseableHttpResponse response;
            response = client.execute(httpPut);
            HttpEntity responseEntity = response.getEntity();
            return EntityUtils.toString(responseEntity);
        } catch (IOException e) {
            return "";
        }
    }
}
